<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-02-03 21:51:15
 * @LastEditTime: 2022-04-13 16:20:26
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:
-->
<template>
  <UploadExcel :onSuccess="onSuccess"></UploadExcel>
</template>

<script setup>
import UploadExcel from '@/components/UploadExcel'
import { USER_RELATIONS, formatDate } from '@/components/UploadExcel/utils'
import { userBatchImport } from '@/api/user-manager'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
const i18n = useI18n()
const router = useRouter()
const onSuccess = async ({ results }) => {
  const updateData = generateData(results)
  await userBatchImport(updateData)
  ElMessage.success(results.length + i18n.t('excel.importSuccess'))
  router.push('/user/manage')
}
const generateData = (data) => {
  const arr = []
  data.forEach(item => {
    const obj = {}
    for (const key in item) {
      if (USER_RELATIONS[key] === 'openTime') {
        obj[USER_RELATIONS[key]] = formatDate(item[key])
        return
      }
      obj[USER_RELATIONS[key]] = item[key]

      arr.push(obj)
    }
  })
  return arr
}
</script>

<style lang="scss" scoped>
</style>
